/**
 * 
 */
package dao;

import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.HashMap;

import model.MonHocM;
import model.MonHocTuyenQuyetM;
import util.DAL;
import util.MyConnectUnit;

public class MonHocTuyenQuyetD {
	private MyConnectUnit conn;

	/**
	 * @throws Exception 
	 * @function constructor MonHocTuyenQuyetD
	 */
	public MonHocTuyenQuyetD() throws Exception {
		super();
		this.conn = DAL.getDAL();
	}
	
	/**
	 * @function get all MonHocTuyenQuyet
	 * @param from
	 * @param to
	 * @return
	 * @throws Exception
	 */
	public ArrayList<MonHocTuyenQuyetM> getAlls(String ten, int from, int to) throws Exception {
		String limit = from + ", " + to;
		ArrayList<MonHocTuyenQuyetM> ls = new ArrayList<MonHocTuyenQuyetM>();
		ResultSet rs = this.conn.Selects(
						"mhtq.id_mon_hoc, mh.ten, mhtq.id_mon_hoc_tq, mht.ten",
						"MON_HOC_TUYEN_QUYET mhtq, MON_HOC mh, MON_HOC mht",
						"mhtq.id_mon_hoc = mh.id_mon_hoc AND mhtq.id_mon_hoc_tq = mht.id_mon_hoc AND mh.ten LIKE '%" + ten + "%'",
						"mh.ten ASC",
						limit);
		MonHocTuyenQuyetM l = null;
		while (rs.next()) {
			l = new MonHocTuyenQuyetM();
			
			l.setId_mon_hoc(rs.getInt("mhtq.id_mon_hoc"));
			MonHocM mh = new MonHocM();
			mh.setTen(rs.getString("mh.ten"));
			l.setTen_mon_hoc(mh);
			
			l.setId_mon_hoc_tq(rs.getInt("mhtq.id_mon_hoc_tq"));
			MonHocM mht = new MonHocM();
			mht.setTen(rs.getString("mht.ten"));
			l.setTen_mon_hoc_tq(mht);
			ls.add(l);
		}
		return ls;
	}
	
	/**
	 * @function get all MonHocTuyenQuyet
	 * @param Condition
	 * @param Limit
	 * @param OrderBy
	 * @return
	 * @throws Exception
	 */
	public ArrayList<MonHocTuyenQuyetM> getAll(String Condition, String Limit, String OrderBy) throws Exception {
		ArrayList<MonHocTuyenQuyetM> ls = new ArrayList<MonHocTuyenQuyetM>();
		ResultSet rs = this.conn.Select("MON_HOC_TUYEN_QUYET", Condition, Limit, OrderBy);
		MonHocTuyenQuyetM l = null;
		while (rs.next()) {
			l = new MonHocTuyenQuyetM();
			l.setId_mon_hoc(rs.getInt("id_mon_hoc"));
			l.setId_mon_hoc_tq(rs.getInt("id_mon_hoc_tq"));
			ls.add(l);
		}
		return ls;
	}

	/**
	 * @function get all MonHocTuyenQuyet with Condition + Limit
	 * @param Condition
	 * @param Limit
	 * @return
	 * @throws Exception
	 */
	public ArrayList<MonHocTuyenQuyetM> getAll(String Condition, String Limit) throws Exception {
		return getAll(Condition, Limit, null);
	}

	/**
	 * @function get all MonHocTuyenQuyet with Condition
	 * @return
	 * @throws Exception
	 */
	public ArrayList<MonHocTuyenQuyetM> getAll(String Condition) throws Exception {
		return getAll(Condition, null);
	}
	
	/**
	 * @function get all MonHocTuyenQuyet no parameter
	 * @return
	 * @throws Exception
	 */
	public ArrayList<MonHocTuyenQuyetM> getAll() throws Exception {
		return getAll(null);
	}
	
	/**
	 * @function search ten MonHocTuyenQuyet
	 * @param ten
	 * @return
	 * @throws Exception
	 */
	public ArrayList<MonHocTuyenQuyetM> searchMHTQ(int id) throws Exception {
		ArrayList<MonHocTuyenQuyetM> list = getAll("id_mon_hoc = " + id);
		return list;
	}
	
	/**
	 * @function get all MonHocTuyenQuyet with search + paging
	 * @param from
	 * @param to
	 * @return
	 * @throws Exception
	 */
	public ArrayList<MonHocTuyenQuyetM> getLimit(int id, int from, int to) throws Exception {
		String limit = from + ", " + to;
		ArrayList<MonHocTuyenQuyetM> list = getAll("id_mon_hoc = " + id, limit);
		return list;
	}

	public int checkExists(int id_mon_hoc, int id_mon_hoc_tq) throws Exception{
		ArrayList<MonHocTuyenQuyetM> l = getAll("id_mon_hoc = " + id_mon_hoc + " AND id_mon_hoc_tq = " + id_mon_hoc_tq);
		return l.size();
	}
	
	/**
	 * function insert MonHocTuyenQuyet
	 * @param mhtq
	 * @return
	 * @throws Exception
	 */
	public boolean Insert(MonHocTuyenQuyetM mhtq) throws Exception {
		HashMap<String, Object> map = new HashMap<String, Object>();
		map.put("id_mon_hoc", mhtq.getId_mon_hoc());
		map.put("id_mon_hoc_tq", mhtq.getId_mon_hoc_tq());
		return this.conn.Insert("MON_HOC_TUYEN_QUYET", map);
	}

	/**
	 * @function delete MonHocTuyenQuyet
	 * @param id_mon_hoc
	 * @param id_mon_hoc_tq
	 * @return
	 * @throws Exception
	 */
	public boolean Delete(int id_mon_hoc, int id_mon_hoc_tq) throws Exception {
		return this.conn.Delete("MON_HOC_TUYEN_QUYET", "id_mon_hoc = " + id_mon_hoc + " AND id_mon_hoc_tq = " + id_mon_hoc_tq);
	}
}
